package com.jeecms.cms.action.admin.assist;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.entity.assist.CmsFlashlogo;
import com.jeecms.cms.entity.main.CmsSite;
import com.jeecms.cms.manager.assist.CmsFileMng;
import com.jeecms.cms.manager.assist.CmsFlashlogoMng;
import com.jeecms.cms.manager.main.CmsLogMng;
import com.jeecms.cms.web.CmsUtils;

@Controller
public class CmsFlashlogoAct {
	private static final Logger log = LoggerFactory
			.getLogger(CmsFlashlogoAct.class);

	@RequestMapping("/flashlogo/v_list.do")
	public String list(Integer queryCtgId, HttpServletRequest request,
			ModelMap model) {
		CmsSite site = CmsUtils.getSite(request);
		List<CmsFlashlogo> list = manager.getList(null);
		model.addAttribute("list", list);
		return "flashlogo/list";
	}

	@RequestMapping("/flashlogo/v_add.do")
	public String add(ModelMap model, HttpServletRequest request) {
		return "flashlogo/add";
	}

	@RequestMapping("/flashlogo/v_edit.do")
	public String edit(Integer id, Integer queryCtgId,
			HttpServletRequest request, ModelMap model) {
		return "flashlogo/edit";
	}

	@RequestMapping("/flashlogo/o_save.do")
	public String save(CmsFlashlogo bean, Integer ctgId,
			HttpServletRequest request, ModelMap model) {
		bean = manager.save(bean);
		fileMng.updateFileByPath(bean.getImage(), true, null);
		log.info("save CmsFlashlogo id={}", bean.getId());
		cmsLogMng.operating(request, "cmsFriendlink.log.save", "id="
				+ bean.getId());
		return "redirect:v_list.do";
	}

	@RequestMapping("/flashlogo/o_update.do")
	public String update(CmsFlashlogo bean, Integer ctgId, Integer queryCtgId,
			String oldLog,
			HttpServletRequest request, ModelMap model) {
		bean = manager.update(bean);
		fileMng.updateFileByPath(oldLog, false, null);
		fileMng.updateFileByPath(bean.getImage(), true, null);
		log.info("update CmsFlashlogo id={}.", bean.getId());
		cmsLogMng.operating(request, "cmsFlashlogo.log.update", "id="
				+ bean.getId());
		return list(queryCtgId, request, model);
	}

	@RequestMapping("/flashlogo/o_priority.do")
	public String priority(Integer[] wids, Integer[] priority,
			Integer queryCtgId, HttpServletRequest request, ModelMap model) {
		manager.updatePriority(wids, priority);
		log.info("update CmsFlashlogo priority.");
		return list(queryCtgId, request, model);
	}

	@RequestMapping("/flashlogo/o_delete.do")
	public String delete(Integer[] ids, Integer queryCtgId,
			HttpServletRequest request, ModelMap model) {
		CmsFlashlogo[] beans = manager.deleteByIds(ids);
		for (CmsFlashlogo bean : beans) {
			fileMng.updateFileByPath(bean.getImage(), false, null);
			log.info("delete CmsFlashlogo id={}", bean.getId());
			cmsLogMng.operating(request, "cmsFlashlogo.log.delete", "id="
					+ bean.getId());
		}
		return list(queryCtgId, request, model);
	}



	@Autowired
	private CmsLogMng cmsLogMng;
	@Autowired
	private CmsFlashlogoMng manager;
	@Autowired
	private CmsFileMng fileMng;
}